package com.yishan.response;

import java.io.Serializable;

public class Response<T> implements Serializable {

    private static final long serialVersionUID = 4058532397681117449L;

    public static final String SUCCESS = "1";

    public static final String SUCCESS_TEXT = "操作成功！";

    public static final String FAIL = "0";

    public static final String FAIL_TEXT = "操作失败！";

    /**
     * 用户信息不存在
     */
    public static final String SUBCODE_101 = "101";
    /**
     * 未知的参数校验异常 框架专用请勿调用
     */
    public static final String SUBCODE_100 = "100";
    /**
     * 参数校验失败
     */
    public static final String SUBCODE_102 = "102";

    /**
     * 业务通用失败
     */
    public static final String SUBCODE_201 = "201";

    /**
     * 业务失败-黄色警告提示
     */
    public static final String SUBCODE_301 = "301";


    private String code;

    private String subCode;

    private String subMsg;

    private String msg;

    private T result;

    public Response() {
        this.code = SUCCESS;
        this.subCode = SUCCESS;
        this.msg = SUCCESS_TEXT;
    }

    public Response(String code, String subCode, String msg) {
        this.code = code;
        this.subCode = subCode;
        this.msg = msg;
        this.subMsg = msg;
    }

    public void addError() {
        this.code = FAIL;
        this.subCode = FAIL;
        this.msg = FAIL_TEXT;
        this.subMsg = FAIL_TEXT;
    }

    public void addError(String msg) {
        this.code = FAIL;
        this.subCode = FAIL;
        this.msg = msg;
        this.subMsg = msg;
    }

    public void addLoginError(String msg) {
        addSubError(SUBCODE_102, msg);
    }

    public void addParamError(String msg) {
        addSubError(SUBCODE_102, msg);
    }

    public void addSubError(String msg) {
        addSubError(SUBCODE_201, msg);
    }

    public void addSubError(String subCode, String subMsg) {
        this.code = FAIL;
        this.subCode = subCode;
        this.msg = subMsg;
        this.subMsg = subMsg;
    }

    public void addSuccess() {
        this.code = SUCCESS;
        this.subCode = SUCCESS;
        this.msg = SUCCESS_TEXT;
        this.subMsg = SUCCESS_TEXT;
    }

    public void addSuccess(T result) {
        this.code = SUCCESS;
        this.subCode = SUCCESS;
        this.msg = SUCCESS_TEXT;
        this.subMsg = SUCCESS_TEXT;
        this.result = result;
    }

    public boolean isSuccess() {
        if (SUCCESS.equals(this.subCode)) {
            return true;
        } else {
            return false;
        }
    }

    public static <T> Response<T> success() {
        return new Response<T>();
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<T>();
        response.setResult(data);
        return response;
    }

    public static <T> Response<T> success(String message, T data) {
        Response<T> response = new Response<T>();
        response.setMsg(message);
        response.setResult(data);
        return response;
    }

    public static <T> Response<T> success(String code ,String message, T data) {
        Response<T> response = new Response<T>();
        response.setCode(code);
        response.setMsg(message);
        response.setResult(data);
        return response;
    }

    public static <T> Response<T> error(String message) {
        Response<T> result = new Response<T>();
        result.addError(message);
        return result;
    }

    public static <T> Response<T> warn(String message) {
        Response<T> result = new Response<T>();
        result.addSubError(SUBCODE_301, message);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getSubCode() {
        return subCode;
    }

    public void setSubCode(String subCode) {
        this.subCode = subCode;
    }

    public String getSubMsg() {
        return subMsg;
    }

    public void setSubMsg(String subMsg) {
        this.subMsg = subMsg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }


}
